/**
 * 
 */
package lablayer.model.MathComponents;

import lablayer.model.ComponentFactory;
import lablayer.model.LabData;
import lablayer.model.Componentable;

/**
 * Component #2
 * 
 * Simple variance of array
 */
class SampleVariance implements MathComponent {
	private LabData result;
	
	private LabData data;
	
	private String componentName = "Выборочная дисперсия";
	
	private String componentId = "2";
	
	public SampleVariance() {
		super();
		this.result = new LabData();
	}
	
	@Override
	public LabData getData() {
		return this.data;
	}

	@Override
	public void calculate() {
		Double[][][] data = this.data.getData();
		Double[] row = null;
		Double mid = 0.0;
		
		if(data.length >= 1) {
			if(data[0].length >= 1) {
				row = data[0][0];
			}
		}
				
		Componentable middleValue = ComponentFactory.createComponent(
				MathComponentFactory.createMiddleValue());
		middleValue.setData(this.data);
		middleValue.calculate();
		mid = ((middleValue.getResult()).getData())[0][0][0];
		
		Double sum = 0.0;
		try {
			assert row != null : "row is null pointer";
			for(int i = 0; i < row.length; i++) {
				sum += (row[i] - mid)*(row[i] - mid);
			}			
		} catch (Exception e) {
			//process exception here
		}
		
		sum = sum / (row.length - 1);
		this.result = new LabData(LabData.parseString(sum.toString()));
	}

	@Override
	public void setData(LabData data) {
		try {
			assert data != null : "data is null pointer";
			this.data = data;
		} catch (Exception e) {
			//process exception here
		}
	}

	@Override
	public String getComponentId() {
		return this.componentId;
	}

	@Override
	public String getComponentName() {
		return this.componentName;
	}

	@Override
	public LabData getResult() {
		return this.result;
	}

	@Override
	public void setParameter(String params, String values) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public LabData getInitData() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

}
